package com.exp.base.format;

import java.sql.Time;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Date;
import java.util.Locale;

import org.apache.commons.lang3.time.DateUtils;
import org.apache.commons.lang3.time.FastDateFormat;
import org.springframework.format.Formatter;

public class SqlTimeFormatter implements Formatter<Time> {
	ArrayList<FastDateFormat> fastDateFormatList;
	
	public SqlTimeFormatter() {
		fastDateFormatList = new ArrayList<FastDateFormat>();
		fastDateFormatList.add(FastDateFormat.getInstance("HH:mm:ss"));
		fastDateFormatList.add(FastDateFormat.getInstance("HHmmss"));
		fastDateFormatList.add(FastDateFormat.getInstance("yyyy-MM-dd HH:mm:ss"));
		fastDateFormatList.add(FastDateFormat.getInstance("yyyyMMddHHmmss"));
	}
	
	public Time parse(String text, Locale locale) throws ParseException {
		if(text==null){
			return null;
		}else{
			for(FastDateFormat fastDateFormat : fastDateFormatList){
				try {
					Date date = fastDateFormat.parse(text);
					return com.exp.base.util.DateUtils.toSqlTime(date);
				} catch (Exception e) {}
			}
			throw new ParseException(text+"can not parese",0);
		}
	}
	
	public String print(Time date, Locale locale) {
		if(date==null){
			return "";
		}else{
			return	fastDateFormatList.get(0).format(date);
		}
	}
}
